package com.funtest.shard.database.policy;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * @Author: flathead
 * @Date: 2021/5/3 11:01
 */
@Slf4j
public class ShardingDBAlgorithm {
    public static final DatabaseShardingAlgorithm DATABASE_SHARDING_ALGORITHM = new DatabaseShardingAlgorithm();
    /**
     * 分库规则
     */
    static class DatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
        @Override
        public String doSharding(Collection<String> databaseNames, PreciseShardingValue<Long> shardingValue) {
            Long orderId = shardingValue.getValue();
            long index = orderId % 2;
            String dataSource = "ds"+index;
//            dataSource="ds0";
            log.info("数据库：{}",dataSource);
            return dataSource;
        }
    }

}
